<template>
  <div class="image" :style="{'height':data.height*40 + (data.height-1)*8 + 'px'}">
    <div class="name">图片选项 :</div>
    <el-upload  :style="{'height':data.height*40 + (data.height-1)*8 -10 + 'px'}" class="avatar-uploader" action="https://jsonplaceholder.typicode.com/posts/" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
      <img v-if="imageUrl" :src="imageUrl" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>
<script>
import imgUrl from '@/assets/logo.png'
export default {
  props: {
    data: {
      type: Object
    }
  },
  data() {
    return {
      imageUrl: "../../../static/logo.png"
    }
  },
  computed: {},
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(this.imageUrl)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
  mounted() {
    console.log(this.data)
  }
}

</script>
<style lang="less">
.image {
  width: 100%;
  height: 100%;
  display: flex;
  border: 1px dashed #000;
  box-sizing: border-box;
  border-radius: 6px;
  height: 88px;
  .name {
    padding: 0 10px;
    line-height: 86px;
  }
  .avatar-uploader {
    flex: 1;
    height: 78px;
    margin-top: 3px;
    margin-right: 10px;
    text-align: center;
    line-height: 76px;
    .el-upload {
      border: 1px dashed #d9d9d9;
      width: 100%;
      height: 100%;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      img{
        width: 80%;
        margin:0 auto;
      }
    }
    .el-upload:hover {
      border-color: #20a0ff;
    }
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100%;
    height: 100%;
    text-align: center;
  }
  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
}

</style>
